package com.controller;

import com.component.LoadBalance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.client.loadbalancer.LoadBalancerRequest;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.List;
import java.util.Random;

@RestController
public class OrderController {
    @Autowired
    private DiscoveryClient discoveryClient;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    @Qualifier("rotationLoadBalancer")
    private LoadBalance loadBalance;

    @GetMapping("/orderToMember")
    public Object orderToMember() {
        // 从注册中心上获取该注册服务列表

        List<ServiceInstance> serviceInstanceList = discoveryClient.getInstances("my-nacos-provider");
        URI uri = loadBalance.getSingleAddres(serviceInstanceList).getUri();
        String result = restTemplate.getForObject( uri+ "/provider/produce?str=李四", String.class);
        return "调用:"+uri+"/n 结果："+result;
    }
    @Bean
//    @LoadBalanced
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
}
